package com.example.happiness.controller.user;

import com.alipay.api.AlipayApiException;
import com.example.happiness.pojo.alipay.AliPay;
import com.example.happiness.result.Result;
import com.example.happiness.utils.AlipayUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "支付宝相关接口")
@Slf4j
@RestController
@RequestMapping("/alipay")
public class UserAlipayController {

    @Autowired
    AlipayUtil alipayUtil;

    @GetMapping("/pay")
    public Result<String> pay(AliPay aliPay) throws AlipayApiException {
        String alipay = alipayUtil.alipay(aliPay);
        log.info("支付宝支付返回结果：{}", aliPay);
        return Result.success(alipay);
    }

    @PostMapping("/notify")
    public void notifyurl(HttpServletRequest request) throws Exception {
        alipayUtil.alipayNotify(request);
    }

    @PostMapping("/refund")
    public void refund(HttpServletResponse response, String orderid, String price) throws IOException, AlipayApiException {
        alipayUtil.refund(response,orderid,price);
        System.out.println(orderid+","+price);
    }
}
